package com.heitian.ssm.controller;

import com.heitian.ssm.model.MessageEntity;
import com.heitian.ssm.service.MessageService;
import com.heitian.ssm.utils.FastJsonUtils;
import com.heitian.ssm.utils.JsonResultUtils;
import com.heitian.ssm.utils.Result;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: mishuai
 * Date: 2018-04-25
 * Time: 19:02
 * Des:
 */
@Controller
@RequestMapping("/admin")
public class MessegeContentController {
    private Logger log = Logger.getLogger(MessegeContentController.class);
    @Resource
    private MessageService messageService;

    @RequestMapping("/messge")
    @ResponseBody
    public void selectByTime(HttpServletRequest request, HttpServletResponse response){

        String youwords=request.getParameter("youwords");
        String contact=request.getParameter("contact");
        MessageEntity messageEntity=new MessageEntity();
        messageEntity.setMessageComment(youwords);
        messageEntity.setUserPhone(contact);
        messageEntity.setCreateTime(new Date());
        messageEntity.setModifyTime(new Date());
        messageEntity.setIsStatus((byte)1);
        log.info("入参内容="+youwords+"联系方式="+contact);

        int i=-1;
        if(messageEntity == null){
            i=-1;
        }

        try {
            i=messageService.insertComment(messageEntity);
        }catch (Exception e){
            log.error(e);
        }

        if(i<=0){
            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_99999","更新数据失败")));
        }else {

            JsonResultUtils.ajaxReturn(response,FastJsonUtils.toJSONString(Result.successReturn(messageEntity)));
        }
    }




    @RequestMapping("/messenger")
    @ResponseBody
    public void selectByBathRoom(HttpServletRequest request, HttpServletResponse response){
        log.info("入参");
        List<MessageEntity> messageEntities=null;
        try{
            messageEntities= messageService.selectByCreatTime();
        }catch (Exception e){
            log.error(e);
            e.printStackTrace();
            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_99999","查询数据失败")));
        }
        JsonResultUtils.ajaxReturnJsonP(request,response,messageEntities);
    }


    @RequestMapping("/messageBackInput")
    @ResponseBody
    public void selectByid(HttpServletRequest request, HttpServletResponse response){
        String messgeID=request.getParameter("messgeID");
        long id=Long.valueOf(messgeID);
        log.info("入参"+id+messgeID);
       MessageEntity messageEntities=null;
        try{
            messageEntities= messageService.selectById(id);
        }catch (Exception e){
            log.error(e);
            e.printStackTrace();
            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_99999","查询数据失败")));
        }
        log.info("出参实体"+FastJsonUtils.toJSONString(messageEntities));
        JsonResultUtils.ajaxReturnJsonP(request,response,messageEntities);
    }

}
